package com.heba.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.heba.greenmulu.R;


/**
 * Created by shenghao on 2015/5/15.
 * <p/>
 * 自定义ProgressView
 */
public class MyProgressDialog {

    public Dialog mDialog;
    private AnimationDrawable animationDrawable = null;

    /**
     * @param context
     * @param message
     */
    public MyProgressDialog(Context context, String message) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.progress_view, null);

        TextView text = view.findViewById(R.id.progress_message);
        text.setText(message);
//        ImageView loadingImage = (ImageView) view.findViewById(R.id.progress_view);
//        loadingImage.setImageResource(R.drawable.loading_animation);
//        animationDrawable = (AnimationDrawable) loadingImage.getDrawable();
//        if (animationDrawable != null) {
//            animationDrawable.setOneShot(false);
//            animationDrawable.start();
//        }

        mDialog = new Dialog(context, R.style.dialog);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(false);

    }

    /**
     * @param context
     * @param message
     * @param cancelListener
     */
    public MyProgressDialog(Context context, String message, DialogInterface.OnCancelListener cancelListener) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.progress_view, null);

        TextView text = view.findViewById(R.id.progress_message);
        text.setText(message);
//        ImageView loadingImage = (ImageView) view.findViewById(R.id.progress_view);
//        loadingImage.setImageResource(R.drawable.loading_animation);
//        animationDrawable = (AnimationDrawable) loadingImage.getDrawable();
//        if (animationDrawable != null) {
//            animationDrawable.setOneShot(false);
//            animationDrawable.start();
//        }

        mDialog = new Dialog(context, R.style.dialog);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setOnCancelListener(cancelListener);

    }

    /**
     * show
     */
    public void show() {
        if (mDialog != null)
            if (!isShowing())
                mDialog.show();
    }

    /**
     * setCanceledOnTouchOutside
     *
     * @param cancel
     */
    public void setCanceledOnTouchOutside(boolean cancel) {
        mDialog.setCanceledOnTouchOutside(cancel);
    }

    /**
     * dismiss
     */
    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
//            animationDrawable.stop();
        }
    }

    /**
     * isShowing
     *
     * @return
     */
    public boolean isShowing() {
        if (mDialog != null) {
            return mDialog.isShowing();
        } else {
            return false;
        }
    }

}
